﻿

namespace StudentAgeSaveEditor.GameData.Wapper
{
    public class RoundMgr : BaseMgr<RoundMgr>
    {
        private RoundModel model;

        public RoundState RoundState { get; private set; }

        public override void Init()
        {
            model = new RoundModel();
            model.Init();
            RoundState = RoundState.FinishPrepare;
        }

        public override void Load()
        {
            RoundState = RoundState.FinishPrepare;
            model = SaveMgrEx.LoadValue<RoundModel>(_isGlobal: false, GetSaveObjKey("model"));
        }

        public override void Save()
        {
            SaveMgrEx.SaveValue(_isGlobal: false, GetSaveObjKey("model"), model);
        }

        public void NextRound()
        {
            if (model.GetRoundCnt() > 0 && Singleton<RoleMgr>.Ins.HasEnoughEnergy(10f))
            {
                HintHelper.ShowConfirm(DescCtrl.GetTxt(11), EndLastRound, null, _showCloseBtn: true, 30, null, null, DescCtrl.GetTxt(217));
            }
            else
            {
                EndLastRound();
            }
        }

        private void EndLastRound()
        {
            OnNextRound();
        }

        private void OnNextRound()
        {
            if (model.GetRoundCnt() > 0 && RoundState == RoundState.FinishPrepare)
            {
                if (IsGameOver())
                {
                    Game.GameOver();
                }
                else
                {
                    Singleton<CommonEvtMgr>.Ins.RoundEnd();
                }

                return;
            }

            RoundState = RoundState.EndLastRound;
            RoundEnd();
            model.AddRound();
           
        }

        public bool IsGameOver()
        {
            if (model.GetYear() == 2007)
            {
                return model.GetSeason() == 2;
            }

            return false;
        }

        public override void RoundEnd()
        {
            Singleton<RoleMgr>.Ins.RoundEnd();
        }

        private void PrepareNextRound()
        {
            RedpointHelper.NewRound();
            Singleton<TipsMgr>.Ins.NewRound();
            Singleton<CommonEvtMgr>.Ins.NewRound();
            Singleton<RoleMgr>.Ins.NewRound();
            Singleton<ShopMgr>.Ins.NewRound();
            Singleton<BagMgr>.Ins.NewRound();
            Singleton<FuncMgr>.Ins.NewRound();
            Game.SaveGame(_isAuto: true);
            AudioMgrEx.PlayNextMusic();
            RoundState = RoundState.FinishPrepare;
          //  EventMgr.Send(101);
        }

        public string GetDateStr()
        {
            return model.GetDateStr();
        }

        public int GetYear()
        {
            return model.GetYear();
        }

        public int GetMonth()
        {
            return model.GetMonth();
        }

        public int GetSeason()
        {
            return model.GetSeason();
        }

        public int GetRoundCnt()
        {
            return model.GetRoundCnt();
        }

        public (int year, int month) Now()
        {
            return (GetYear(), GetMonth());
        }

        public (int year, int season) NowSeason()
        {
            return (GetYear(), GetSeason());
        }

        public void SetTime(int _year, int _month)
        {
            model.SetTime(_year, _month);
        }

        public void OnPreRoundFinish()
        {
            RoundState = RoundState.PrepareNextRound;
            OnNextRound();
        }

        public override void NewRound()
        {
        }

    
    }
}
